package com.hz.house.service;

import com.alibaba.fastjson.JSON;
import com.hz.house.common.wxmini.WxMaSubscribeMessage;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信模板消息接口
 *
 * @author hsk
 */
@Service
@Log4j2
public class WxMaSubscribeService {

    @Resource
    private RestTemplate restTemplate;
    @Resource
    private WxMiniService wxMiniService;

    /**
     * 获取当前帐号下的个人模板列表.
     */
    String TEMPLATE_LIST_URL = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate";

    /**
     * 删除帐号下的某个模板.
     */
    String TEMPLATE_DEL_URL = "https://api.weixin.qq.com/wxaapi/newtmpl/deltemplate";

    /**
     * 发送订阅消息
     */
    String SUBSCRIBE_MSG_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send";

    /**
     * 积分明细页面
     */
    String SCORE_DETAIL_PAGE = "pages/personal/integraldetail/main";

    /**
     * 系统消息列表
     */
    String MESSAGE_DETAIL_PAGE = "pages/personal/message/index/main";

    /**
     * 积分通知订阅消息模板编码
     */
    String SCORE_DETAIL_TEMPLATE_CODE = "gcXFDn35H-Y5AYC98GeVpUZ6MZ4ThaM6eL1PXA-Ni1U";

    /**
     * 访客咨询提醒订阅消息模板编码
     */
    String USER_ASK_REMIND = "oNpJuLKdFpJbJUFBXVsOKTnPQhT4OQbr1DZtp1mZH1E";


    /**
     * <pre>
     * 获取当前帐号下的个人模板列表
     *
     * 详情请见: <a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getTemplateList.html">获取当前帐号下的个人模板列表</a>
     * 接口url格式: GET https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token=ACCESS_TOKEN
     * </pre>
     *
     * @return .
     */
    public List<TemplateInfo> getTemplateList() {
        String result = this.restTemplate.getForObject(wxMiniService.addMiniAccessToken(TEMPLATE_LIST_URL), String.class);
        log.info("=======获取当前帐号下的个人模板列表数据返回:{}", result);
        Map<String, Object> map = JSON.parseObject(result, Map.class);
        if ((int) map.get("errcode") != 0) {
            log.error("获取小程序模板列表失败:{}", map.get("errmsg"));
            return new ArrayList<>();
        }
        return JSON.parseArray(JSON.toJSONString(map.get("data")), TemplateInfo.class);
    }

    /**
     * <pre>
     * 删除帐号下的某个模板
     *
     * 详情请见: <a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.deleteTemplate.html">删除帐号下的个人模板</a>
     * 接口url格式: POST https://api.weixin.qq.com/wxaapi/newtmpl/deltemplate?access_token=ACCESS_TOKEN
     * </pre>
     *
     * @param templateId 要删除的模板id
     * @return 删除是否成功
     */
    public void delTemplate(String templateId) {
        Map<String, Object> params = new HashMap<>();
        params.put("priTmplId", templateId);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<>(JSON.toJSONString(params), headers);
        String result = restTemplate.postForObject(wxMiniService.addMiniAccessToken(TEMPLATE_DEL_URL), entity, String.class);
        log.info("=======删除小程序模板数据返回:{}", result);
    }

    /**
     * <pre>
     * 发送订阅消息
     *
     * 详情请见: <a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html">发送订阅消息</a>
     * 接口url格式: POST https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN
     * </pre>
     *
     * @param subscribeMessage 发送对象
     */
    public void sendSubscribeMsg(WxMaSubscribeMessage subscribeMessage) {
        //关键字验证
        if (!wxMiniService.checkContent(JSON.toJSONString(subscribeMessage))) {
            log.warn("关键字校验失败:{}", subscribeMessage);
            return;
        }
        String result = restTemplate.postForObject(wxMiniService.addMiniAccessToken(SUBSCRIBE_MSG_SEND_URL), JSON.toJSON(subscribeMessage), String.class);
        log.info("小程序发送订阅消息数据返回:{}", result);
    }

    @Data
    class TemplateInfo {
        private String priTmplId;
        private String title;
        private String content;
        private String example;
        private int type;
    }

    @Data
    class PubTemplateKeyword {
        private int kid;
        private String name;
        private String example;
        private String rule;
    }

}
